package net.steam.easyjoboa.web.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import net.steam.easyjoboa.biz.AddressBookGroupBiz;
import net.steam.easyjoboa.entity.AddressBook;
import net.steam.easyjoboa.entity.AddressGroup;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.PageBeans;

public class AddressBookGroupAction {
	private int pageIndex=1;
	private int pageSize=5;
	private PageBeans pageBean;
	private AddressBookGroupBiz addressBookGroupBiz;
	private List<AddressGroup> list;
	private List<AddressBook> list2;
	private String addName=null;
	private int id;
	private AddressGroup addressGroup;
	private AddressBook addressBook;
	private String name;
	private String desc;
	private int[] checkbox;
	private int count=0;
	Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
	
	@Action(value="listAddress",results={@Result(name="success",location="../../message/address.jsp")})
	public String page(){
		if(pageBean==null){
			pageBean = new PageBeans();
		}
		pageBean.setCurrentPage(pageIndex);	
		if(pageSize<=0){
			pageSize=5;
		}
		pageBean.setPageSize(pageSize);
		list=addressBookGroupBiz.getGroups(employee.getUserId(), pageBean,addName);		
		return "success";
	}
	
	
	@Action(value="add",results={@Result(name="success",location="listAddress",type="redirectAction"),
								@Result(name="input",location="../../message/add.jsp")})
	public String addGroup(){
		AddressGroup addressGroup = new AddressGroup();
		list=addressBookGroupBiz.getListByName(name);
		if(list.size()==0){
			addressGroup.setAddressGroupName(name);
			addressGroup.setAddressGroupDesc(desc);
			addressGroup.setEmployee(employee);		
			addressBookGroupBiz.addGroup(addressGroup);
			return "success";
		}else{
			return "input";
		}
	}
	
	@Action(value="groupById",results={@Result(name="success",location="../../message/update.jsp")})
	public String getGroupById(){
		addressGroup= addressBookGroupBiz.getGroupById(id);
		return "success";
	}
	
	@Action(value="updateGroup",results={@Result(name="success",location="listAddress",type="redirectAction")})
	public String update(){
		addressGroup= addressBookGroupBiz.getGroupById(id);
		addressGroup.setAddressGroupName(name);
		addressGroup.setAddressGroupDesc(desc);
		addressBookGroupBiz.update(addressGroup);
		return "success";
	}
	@Action(value="getByUserId",results={@Result(name="success",location="../../message/personalAddress.jsp")})
	public String getByUserId(){
		list=addressBookGroupBiz.getAddressGroupByUserId(employee.getUserId());
		return "success";
	}
	@Action(value="getgroupsById",results={@Result(name="success",location="../../message/addPersonal.jsp")})
	public String getByUserIds(){
		list=addressBookGroupBiz.getAddressGroupByUserId(employee.getUserId());
		return "success";
	}
	@Action(value="deleteGroup",results={@Result(name="success",location="listAddress",type="redirectAction")})
	public String del(){
		addressGroup=addressBookGroupBiz.getGroupById(id);			
		addressBookGroupBiz.delete(addressGroup);
		return "success";
	}
	
	
	
	
	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}


	public PageBeans getPageBean() {
		return pageBean;
	}


	public void setPageBean(PageBeans pageBean) {
		this.pageBean = pageBean;
	}


	public void setaddressBookGroupBiz(AddressBookGroupBiz addressBookGroupBiz) {
		this.addressBookGroupBiz = addressBookGroupBiz;
	}


	public List<AddressGroup> getList() {
		return list;
	}


	public void setList(List<AddressGroup> list) {
		this.list = list;
	}


	public String getAddName() {
		return addName;
	}


	public void setAddName(String addName) {
		this.addName = addName;
	}
	
	
	public AddressGroup getAddressGroup() {                                     
		return addressGroup;
	}
	
	
	public void setAddressGroup(AddressGroup addressGroup) {
		this.addressGroup = addressGroup;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public AddressBook getAddressBook() {
		return addressBook;
	}
	public void setAddressBook(AddressBook addressBook) {
		this.addressBook = addressBook;
	}
	public List<AddressBook> getList2() {
		return list2;
	}
	public void setList2(List<AddressBook> list2) {
		this.list2 = list2;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}

	public int[] getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(int[] checkbox) {
		this.checkbox = checkbox;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public Employee getEmployee() {
		return employee;
	}


	public void setEmployee(Employee employee) {
		this.employee = employee;
	}


	public void setAddressBookGroupBiz(AddressBookGroupBiz addressBookGroupBiz) {
		this.addressBookGroupBiz = addressBookGroupBiz;
	}	
}
